package cn.source.system.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.source.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.enums.BusinessType;
import cn.source.system.domain.AaCategory;
import cn.source.system.service.IAaCategoryService;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.common.core.page.TableDataInfo;

/**
 * 分类管理Controller
 * 
 * @author sourcebyte.vip
 * @date 2023-08-04
 */
@RestController
@RequestMapping("/cms/category")
public class AaCategoryController extends BaseController
{
    @Autowired
    private IAaCategoryService aaCategoryService;

    /**
     * 查询分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('cms:category:list')")
    @GetMapping("/list")
    public AjaxResult list(AaCategory aaCategory)
    {
        List<AaCategory> list = aaCategoryService.selectAaCategoryList(aaCategory);
        return AjaxResult.success(list);
    }

    /**
     * 导出分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('cms:category:export')")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AaCategory aaCategory)
    {
        List<AaCategory> list = aaCategoryService.selectAaCategoryList(aaCategory);
        ExcelUtil<AaCategory> util = new ExcelUtil<AaCategory>(AaCategory.class);
        util.exportExcel(response, list, "分类管理数据");
    }

    /**
     * 获取分类管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(aaCategoryService.selectAaCategoryById(id));
    }

    /**
     * 新增分类管理
     */
    @PreAuthorize("@ss.hasPermi('cms:category:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AaCategory aaCategory)
    {
        if(ObjectUtil.isNull(aaCategory.getCategoryPid())||aaCategory.getCategoryPid()==0L){
            aaCategory.setCategoryLeve(0L);
        }else{
            AaCategory category = aaCategoryService.selectAaCategoryById(aaCategory.getCategoryPid());
            aaCategory.setCategoryLeve(category.getCategoryLeve()+1L);
        }
        aaCategory.setCreateDate(new Date());
        return toAjax(aaCategoryService.insertAaCategory(aaCategory));
    }

    /**
     * 修改分类管理
     */
    @PreAuthorize("@ss.hasPermi('cms:category:edit')")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AaCategory aaCategory)
    {
        return toAjax(aaCategoryService.updateAaCategory(aaCategory));
    }

    /**
     * 删除分类管理
     */
    @PreAuthorize("@ss.hasPermi('cms:category:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aaCategoryService.deleteAaCategoryByIds(ids));
    }
}
